package com.sgb.kjwl.view.ui.fragment.home;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.kjwl.R;

import com.swgk.core.base.di.AppComponent;
import com.swgk.core.databinding.FragmentMenuBinding;
import com.swgk.core.base.AppFragment;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.kjwl.view.ui.activity.MainActivity;
import com.sgb.kjwl.view.ui.adapter.home.HostMenuAdapter;
import com.swgk.core.recyclerview.adapter.PageRecycleAdapter;
import com.swgk.core.recyclerview.decoration.RecyclerViewItemDecoration;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("ValidFragment")
@Deprecated
public class EMMenuFragment extends AppFragment {
    public static final String TAG = "MenuFra";
    public static final String KEY_DATA = "data";
    public static final String COLUMN_COUNT = "colum_count";
    public static final String ROW_COUNT = "row_count";
    public static final String PAGE = "page";

    FragmentMenuBinding binding;
    private MainActivity activity;
    private HostMenuAdapter adapter;
    private List<MenuEntity> datas;
    private int colum = 4;
    private int row = 2;
    private int page;

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof MainActivity) {
            activity = (MainActivity) context;
        }
    }

    public static EMMenuFragment newInstance(List<MenuEntity> type, int colum, int row, int page) {
        EMMenuFragment fragment = new EMMenuFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(KEY_DATA, (ArrayList<? extends Parcelable>) type);
        bundle.putInt(COLUMN_COUNT, colum);
        bundle.putInt(ROW_COUNT, row);
        bundle.putInt(PAGE, page);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_menu, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        datas = getArguments().getParcelableArrayList(KEY_DATA);
        colum = (int) getArguments().get(COLUMN_COUNT);
        row = (int) getArguments().get(ROW_COUNT);
        page = (int) getArguments().get(PAGE);

        init();
    }

    private void init() {
        adapter = new HostMenuAdapter(getActivity(), null);
        binding.rlvMenu.setAdapter(adapter);
        binding.rlvMenu.setLayoutManager(new GridLayoutManager(getActivity(), colum));
        binding.rlvMenu.addItemDecoration(new RecyclerViewItemDecoration(getResources().getDimension(R.dimen.dp6)));
        adapter.setCount(colum * row);
        adapter.setDatas(datas);
        adapter.setOnItemClick(new PageRecycleAdapter.OnItemClick<MenuEntity>() {
            @Override
            public void onClick(MenuEntity entity, int position) {
                if (onItemClickCallBack != null) {
                    onItemClickCallBack.onClick(entity, position);
                }
            }
        });

    }

    public OnItemClickCallBack onItemClickCallBack;

    public interface OnItemClickCallBack {
        void onClick(MenuEntity entity, int position);
    }

    public void setOnItemClickCallBack(OnItemClickCallBack onItemClickCallBack) {
        this.onItemClickCallBack = onItemClickCallBack;
    }
}
